package com.example.mes_boot.controller;

import com.example.mes_boot.pojo.checkpoint;
import com.example.mes_boot.pojo.factory;
import com.example.mes_boot.service.Indexservice;
import com.example.mes_boot.service.imp.addservieimpl;
import com.example.mes_boot.service.imp.deleservieimpl;
import com.example.mes_boot.service.imp.updataservieimpl;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/factory")
public class factorycontroller {
    @Autowired
    private Indexservice indexservice;
    @Autowired
    private deleservieimpl deleservieimpl;
    @Autowired
    private addservieimpl addservieimpl;
    @Autowired
    private  updataservieimpl updataservieimpl;
    //加载接口
    @PostMapping("/load")
    private List<factory> find(){ return indexservice.findByfactory(); }
    //分页接口
    @PostMapping("/page")
    private List<factory> findby(@RequestBody factory point) {
        int currPage=point.getPage();//当前页码
        int pageSize=8;//当前页显示记录数量
        //注意：currPage和start别搞错了，一个表示当前页码，一个是从第几行读取记录
        int start = (currPage-1)*pageSize;//计算从第几行读取记录
        RowBounds rowBounds = new RowBounds(start,pageSize);
        return indexservice.findByfactorypage(rowBounds);
    }
    //模糊查询
    @PostMapping("/sele")
    private  List<factory> sele(@RequestBody factory sele){
        return indexservice.findByfactory(sele.getName());
    }
    //物料查询
    @PostMapping("/upload")
    private  List<factory> upload(@RequestBody factory id){
        return  indexservice.findByload(id.getId());
    }
    //物料删除
    @PostMapping("/delewl")
    private void delewl(@RequestBody factory id){
        deleservieimpl.delewl(id.getId(),id.getPage());
    }
    //查询
    @PostMapping("/equipment")
    private List<factory> equipment(){
        return  indexservice.equipment();
    }
    //修改接口
    @PostMapping("/updata")
    private void   updata(@RequestBody factory id){
        addservieimpl.updatafactory(id.getId(),id.getPage()); ;
    }
    //删除接口
    @PostMapping("/dele")
    private  void  dele(@RequestBody factory id){
        deleservieimpl.delefactory(id);
    }
    //查找
    @PostMapping("/selewl")
    private  List<factory> selewl(@RequestBody factory wl){
       List <factory> w= indexservice.find(wl.getCode());
       List <factory> wuliao=new ArrayList<>();
       List<factory> sj;
        for (int i = 0; i < w.size(); i++) {
            sj=indexservice.findwl(null,null,null,null,w.get(i).getCode());
            for (int y =0;y<sj.size();y++) {
                wuliao.add(sj.get(y));
            }
        }
       return wuliao;
    }
    //修改
    @PostMapping("/up")
    private void  up(@RequestBody factory id){
        System.out.println(id);
        updataservieimpl.up(id.getId(),id.getPage());
    }
    @PostMapping("/addapi")
    private factory  addapi(@RequestBody factory id){
        System.out.println(id);
        addservieimpl.addwl(id);
        System.out.println(id);
       return id;
    }
}
